/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.systemcar.dao;

import br.com.systemcar.model.Posto;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import br.com.systemcar.model.PostoCombustivel;
import br.com.systemcar.model.PostoCombustivelPK;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.ejb.Stateful;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

/**
 *
 * @author danilo
 */
@Stateful
public class PostoCombustivelDao implements Serializable {

    @PersistenceContext(unitName = "SystemcarPU")
    EntityManager em;
    @Resource
    UserTransaction utx;

    public void salvar(PostoCombustivel pc) {
        em.persist(pc);
    }

    public void alterar(PostoCombustivel pc) {
        em.merge(pc);

    }

    public void remover(PostoCombustivel pc) {
        //em.remove(pc);

        if (pc == null) {
            return;
        }

        PostoCombustivel target = pc;
        //Posto posto = pc.getPosto();
        try {
            if (!em.contains(pc)) {
               // utx.begin();
                target = em.merge(pc);
                //utx.commit();
            }

           // utx.begin();
            
            em.remove(target);
           // utx.commit();
        } catch (Exception e) {
             Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null,e);
        }

//        PostoCombustivelPK pk = pc.getPostoCombustivelPK();
//        em.remove(pk);


    }

    public List<PostoCombustivel> findByPosto(int id) {
        List<PostoCombustivel> lista = new ArrayList<PostoCombustivel>();
        Query query = em.createNamedQuery("PostoCombustivel.findByIdPosto");
        query.setParameter("idPosto", id);

        lista = query.getResultList();
        return lista;

    }
}
